﻿ 
$(function () {
    mui.init(); 
    mui('.mui-bar-tab').on('tap', 'a', function () {
        document.location.href = this.href;
    });
    gethycode();
})

var rows;//全局合约数据
//是否打断加载
let isbreakload = false;
//读取天勤行情数据
function gethycode() {
    let dataobj;
    $.getJSON('http://122.14.208.164:19000/index?m=test&a=get_hy&ins_class=FUTURE', function (data) {
        let hycodeList = data.data.sort(function (a, b) { return a.localeCompare(b) })
        let total = hycodeList.length
        //console.log('rows', hycodeList)
        rows = [];
        let filterdata = [{ value: '', text: '全部' }];
       //只加载合约代码的rows，同时确定合约分类数据filterdata
        $.each(hycodeList, function (index, item) {
            rows.push({
                "id": index,
                "JYS": item.split('.')[0],
                "code": item.split('.')[1],
                "last_price": '',
                "volume": '',
                "amount": '',
                "open_interest": '',
                "bid_price1": '',
                "ask_price1": '',
                "open": '',
                "close": '',
                "highest": '',
                "lowest": '',
                "settlement": '',
                "pre_close": '',
                "pre_settlement": ''
            });

            let tempitem = item.split('.')[0];
            let has = false;
            $.each(filterdata, function (index, item) {
                if (item.value == tempitem) {
                    has = true;
                }
            })
            if (!has) {
                filterdata.push({ value: tempitem, text: tempitem });
            } 
        })
        //合约分类数据加载到页面
        $.each(filterdata, function (index, item) {
            if (item.value.length > 0) {
                $("#filterbtngroup").append('<input type="button" class="cxbtn" value="' + item.value + '" onclick="FilterJYS(\'' + item.value + '\')" />')
            }
            else {
                $("#filterbtngroup").append('<input type="button" class="cxbtn" value="全部" onclick="FilterJYS(\'\')" />')
            }
        })

        dataobj = { "total": total, "rows": rows }
        //初始化数据表格
        $("#dg1").datagrid({
            singleSelect: true,
            fit: true,
            data: dataobj,
            frozenColumns: [[
                { field: 'JYS', title: '交易所', width: '80' },
                { field: 'code', title: '合约代码' },
            ]],
            columns: [[
                { field: 'last_price', title: '最新价' },
                { field: 'volume', title: '成交量' },
                { field: 'amount', title: '成交额' },
                { field: 'open_interest', title: '持仓量' },
                { field: 'bid_price1', title: '买价' },
                { field: 'ask_price1', title: '卖价' },
                { field: 'open', title: '开盘' },
                { field: 'close', title: '收盘' },
                { field: 'highest', title: '最高' },
                { field: 'lowest', title: '最低' },
                { field: 'settlement', title: '结算价' },
                { field: 'pre_close', title: '昨收' },
                { field: 'pre_settlement', title: '昨结' }
            ]],
            onClickRow: function (index, row) {
                window.location.href = '/Mobile/MIndex/Kline?code=' + row.JYS+'.'+row.code;
            }
        })
        //加载行情数据
        LoadHqGrid();
         
    })
}

function FilterJYS(keys) { 
    var newArr = rows.filter(function (p) {
        return p.JYS == keys;
    });
    
    if (keys.length > 0) {
        $('#dg1').datagrid('loadData', newArr); 
    }
    else { 

        $('#dg1').datagrid('loadData', rows); 
    }
    LoadHqGrid(); 
    return newArr;
}
 

function LoadHqGrid() {
    //isbreakload = false;
    let GridDatas = $("#dg1").datagrid("getData");
    //console.log('getData', GridDatas);
    //更新表格 加载行情数据
    $(GridDatas.rows).each(function (index, item) {
        if (!isbreakload) {
            console.log(index)
            $.getJSON('http://122.14.208.164:19000/index?m=test&a=get_real', { symbol: item.JYS + '.' + item.code }, function (data) {
                var data2 = data.data.replace(/'/g, '"').replace(/nan/g, '" "').replace(/False/g, 'false').replace(/True/g, 'true');
                var json = eval('(' + data2 + ')');
                //console.log('getData', $("#dg1").datagrid("getData"));
                //console.log('GridDatas', GridDatas)
                let iseq = true;
                let gridrows = $("#dg1").datagrid("getData").rows;
                $.each(GridDatas.rows, function (index, item) {
                    if (item.JYS != gridrows[index].JYS || item.code != gridrows[index].code) {
                        iseq = false;
                    }
                })
                if (iseq) {
                    $('#dg1').datagrid('updateRow', {
                        index: index,
                        row: {
                            "last_price": json.last_price,
                            "volume": json.volume,
                            "amount": json.amount,
                            "open_interest": json.open_interest,
                            "bid_price1": json.bid_price1,
                            "ask_price1": json.ask_price1,
                            "open": json.open,
                            "close": json.close,
                            "highest": json.highest,
                            "lowest": json.lowest,
                            "settlement": json.settlement,
                            "pre_close": json.pre_close,
                            "pre_settlement": json.pre_settlement
                        }
                    });
                }
            });

        }

    })

}

function GroupBy(arr, key) {
    var obj = {};
    for (let i = 0; i < arr.length; i++) {
        if (obj[arr[i][key]]) {
            obj[arr[i][key]].push(arr[i]);
        } else {
            obj[arr[i][key]] = new Array();
            obj[arr[i][key]].push(arr[i]);
        }
    }
    return obj;
}



function getMyHy() {
    $.post('/Mobile/MIndex/get_myhy', function (data) {
        var json = eval('(' + data + ')');
        var jstr = '';
        var trstr = '';
        $.each(json, function (index, item) {
            jstr += '<div>恒生指数<div class="text" onclick="KLine(\'' + item.F_ItemCode + '\')">' + item.F_ItemCode + '</div></div> ';
            trstr += '<tr id="' + item.F_Id + '"></tr>';
        })
        $("#hyhq").html(trstr);
        $.each(json, function (index, item) {
            gethyhq(item);
        })

        $("#hytt").html(jstr)
    })
}

function KLine(item) {
    window.location.href = '/Mobile/MIndex/Kline?code=' + item;
}
function gethyhq(item) {
    var code = item.F_ItemCode;
    $.getJSON('http://122.14.208.164:19000/index?m=test&a=get_real', { symbol: item }, function (data) {
        var data2 = data.data
            .replace(/'/g, '"')
            .replace(/nan/g, '"nan"')
            .replace(/False/g, 'false')
            .replace(/True/g, 'true');
        var json = eval('(' + data2 + ')');
        //console.log('json',json)
        var str = "";
        str = str + '<td>'
            + json.last_price + '</td><td>'

            + json.volume + '</td><td>'
            + json.amount + '</td><td>'

            + json.open_interest + '</td><td>'
            + json.bid_price1 + '</td><td>'
            + json.ask_price1 + '</td><td>'

            + json.open + '</td><td>'
            + json.close + '</td><td>'
            + json.highest + '</td><td>'
            + json.lowest + '</td><td>'

            + json.settlement + '</td><td>'

            + json.pre_close + '</td><td>' +

            + json.pre_settlement + '</td>';
        var fid = "#" + item.replace('.', '');
        console.log('str', str)
        $(fid).html(str);

    });

}